perm filename MACRO.TEX[WEB,ALS] blob
sn#743863 filedate 1984-02-27 generic text, type C, neo UTF8
COMMENT ā VALID 00003 PAGES
C REC PAGE DESCRIPTION
C00001 00001
C00002 00002 \font\ninerm=amr9 \font\eightrm=amr8 \font\sixrm=amr6 % \font\mc=cmcsc
C00004 00003 \centerline{\bf \TeX\ Macros Made Easy}
C00020 ENDMK
Cā;
\font\ninerm=amr9 \font\eightrm=amr8 \font\sixrm=amr6 % \font\mc=cmcsc
\font\seal=stan70
\font\ttt=amtt9
\def\TeX{T\kern-.1667em\lower.5ex\hbox{E}\kern-.125em X}
\def\TEX{T\kern-.1667em\lower.5ex\hbox{E}\kern-.125em X}
\def\La{L\kern-.1667em\raise.5ex\hbox{a}\kern-.1em}
\magnification=1200
\def\undertext#1{$\underline{\hbox{#1}}$}
\def\\{{\tt \char'134}}
\def\up{{\sy \char'42}}
\def\down{{\sy \char'43}}
\def\caret{{\char'17}}
\parskip 10pt plus 1pt
\parindent 0pt
\def\cs{{\sl control sequence}}
\def\css{{\sl control sequences}}
\def\Css{{\sl Control sequences}}
\def\lbr{{\tt \char'173}} \def\rbr{{\tt \char'175}} \def\rrbr{{\tt \char'175\char'175}}
\def\und{\tt \char'137}
\def\til{\tt \char'176}
\def\sup{\tt \char'136}
\def\la{\tt \char'074}
\def\ra{\tt \char'076}
\def\loterms{\hbox{lower order terms}}
\centerline{\bf \TeX\ Macros Made Easy}
{\bf Introduction}
This is an introductory manual to the art of writing macros for \TeX. It
is written for the advanced beginner who has mastered the ``First Grade
\TeX'' manual and now wants to progress one more step along the way toward
becoming a \TeX pert. This manual will not make you into a \TeX pert, but
it will help to dispel some of the mystery that seems to surround this
matter of \TeX\ macros. Rather than being a condensation of a portion of
``The \TeX book'', this manual is, if anything, an expansion of the
material in Chapter~20 of ``The \TeX book'', with a liberal sprinkling of
ideas culled from Appendix~B and Appendix~D.
Writing \TeX\ macros is really not all that difficult. While the writing
of a complete set of macros, such as those contained in ``The \La\TeX\
Document preparation System'' by Leslie Lamport or in ``The Joy of \TeX''
by Michael Spivak, is not a task to be undertaken lightly, do not
dispair. The \TeX\ user who has mastered the rudiments of \TeX, as
outlined in ``First Grade \TeX'' is well prepared to go on the task of
learning
either 1) how to writing his or her own macros, to suppliment those provided by {\tt
PLAIN.TEX}, if needs are simple, or 2) how to use an already prepared macro
set and to hand tailoring a small number of the macros that are contained
in the chosen set so as to make them more suitable for his or her own
particular needs.
{\bf A Brief Review}
You have been introduced to five basic ideas, which we will now review with
slightly more precision as compared to the treatment in ``First Grade \TeX''.
1) Macro definitions all begin with the \cs\/
`{\tt \\def}' followed by the new name and then the meaning to be assigned to
this new name (enclosed in braces that are not a part of the definition).
If braces are wanted as a part of the definition they must be added.
First Grade \TeX\ used as an example the definition:
\smallskip
\centerline{\tt \\def\\loterms\lbr \\hbox\lbr \\rm lower order terms\rrbr.}
\smallskip
and went on to state that, once you had defined this macro, you could then
type
$$\hbox{{\tt \$x\ā{}3+\\loterms\$}\qquad to get \qquad}xā3+\loterms.$$
2) Macros normally use other \css\/ (including macros
that you have defined) in their definitions. In fact, the only reason for
using macros is to make it easy to remember and to execute a complicated
sequence of \css\/
that would be hard to remember and hard to type.
3) Macros may involve conditionals as evidenced by the following:
$$\vbox{\tt {\halign{#\hfil\cr
\\headline=\lbr\\ifodd\\pageno\\rightheadline \\else\\leftheadline\\fi\rbr\cr
\\def\\rightheadline\lbr\\tenrm\\hfil A Beginner's \\TeX\\ Manual\\hfil\\folio\rbr\cr
\\def\\leftheadline\lbr\\tenrm\\folio\\hfil First Grade \\TeX \\hfil\rbr\cr
}}}$$
This conditional is of the form:
$$\hbox{{\tt\\if}\la true text\ra{\tt\\else}\la false text\ra{\tt\\fi}}$$
We will have occasion to discuss alternate forms later.
4) Macro definitions that are used in other macro definitions need not
necessarily be defined before they are so used (but they must, of course,
have been defined before the referencing definition is evoked. This
feature was not explicitly stated but it was used in defining a varient
form of an output format as was shown in the example
3) above. A clear distinction must be drawn between defining a macro and evoking
it.
We will have to explore this timing matter in some detail a
little later.
5) Macros can be defined in terms of {\sl parameters} and the
{\sl arguments} that will be substituted for these {\sl parameters} can be
supplied when the macro is evoked. Obviously, different {\sl parameters} can
be used each different time that the macro is used.
The example previously cited was:
$$\hbox{\tt\\def\\centerline\#1\lbr \\line\lbr \\hss\#1\\hss\rrbr.}$$
which I had occasion to use on the previous page when I wrote:
$$\hbox{\tt \\centerline\lbr\\bf \\TeX\\ Macros Made Easy\rbr}.$$
\def\row#1{(#1_1,\ldots,#1_n)}
A somewhat more involved example used in ``The \TeX book'' is:
$$\hbox{\tt \\def\\row\#1\lbr(\#1\und{}1,\\ldots,\#1\und n)\rbr}$$
Having made such a definition, you can type (when in math mode):
$$\hbox{{\tt \\row x}\quad to get\quad $\row x$\qquad and\qquad
{\tt \\row y}\quad to get\quad $\row y$}.$$
One difference should be noted: in the earlier example we used braces to enclose
the argument, since it was more than one character in length while in this example
no braces were needed. Actually, you can write {\tt \\row \lbr x\rbr} in place of
{\tt \\row x} if you want and \TeX\ will obligingly strip of the braces. Of course,
if you want braces to appear, you can type:
$$\hbox{{\tt \\row\lbr\\\lbr x\\\rbr\rbr}\quad
to get\quad $\row{\{x\}}$\qquad or\qquad
{\tt \\\lbr \\row x\\\rbr} \quad to get\quad $\{\row x\}$.}$$
Actually, up to 9 parameters can be so specified.
The definition might then take the form \quad {\tt\\def\\zzz\#1\#2\#3}\lbr a
complex expression in which {\tt\#1}, {\tt\#2}, and {\tt\#3} can be used in any order and
repeatedly if needed\rbr. The parameters, as listed following the name of
the control sequence, must, however, be in serial order.
{\bf This Matter of Timing}
As mentioned in 4) above, a sharp distinction must be drawn between the
act of defining a macro and the act of evoking it.
Normally, macros
are not interpreted at the time they are initially defined. Normally, macros
are interpreted when they are evoked and this later process is called
`expanding the macro'. Much of the mystery of
macros results from the startling results that can be achieved by taking
full advantage of this feature. Just remember that \TeX\ exercises no
look-ahead (at least for an ordinary {\tt\\def}) and that it pushes
blindly ahead and does one operation after another as required.
When \TeX\
encounters a macro definition that you have typed, it simply converts this
definition into an internal notation involving things called `{\sl tokens}'.
In fact, everything that you type is converted into {\sl tokens}.
A token is either a) a single character with an appended catagory code that
tells if the character is a letter, or a number, or any one of quite a few
other categories of special characters that are handled differently, there
being 16 different categories, or b) a \cs.
Eventually you
will need to know something about these different categories because \TeX\
treats characters in different categories differently, but for the present
you need to know only that these categories exist. The reason for
clasifying the input characters is, of course, so that \TeX\ can later
decide what to do about the next token that it encounters without having
to take time out to find out its category. Once a category code has been assigned
to an input character the assignment is permanent. \Css\/ do not carry category
definitions but they are expressed in terms of {\sl tokens}.
\def\a{\b}
\def\b{A\def\a{B\def\a{\b}}}
Let us demonstrate this feature right now.
Suppose you type a file containing the following;
$$\hbox{\tt \\def\\a\lbr\b\rbr\qquad
def\\b\lbr A\\def\\a\lbr B\\def\\a\lbr\\b\rbr\rbr}$$
What do you suppose happens when you later type a sequence of \\a's? If
there is any doubt in your minds, then try it.
Actually it is really quite simple. On receiving the
first to-be-executed {\tt \\a}, \TeX\ looks up its definition and finds
that it must look up {\tt\\b}, which it proceeds to execute. Th first
thing it does is to send off the letter `A' to be readied for printing.
It then redefines {\tt\\a} so that now the definition of {\tt\\a} is now
{\tt \\def\\a\lbr B\\def\\a\lbr\\b\rbr\rbr} but, of course, it does not
re-execute {\tt\\a}. On the next {\tt\\a} command a {\tt B} is printed
and no recourse to the definition of {\tt\\b} is made but the definition
of {\tt \\a} is also changed back to its original settion and thereafter the
process is repeated.
Much of this apparent mysticism seems to reside in the fact that the
novice is disposed to think that the facility must be quite complicated
when in fact the power of the macro facility resides in a very few
extremely simple rules which \TeX\ applies in a very mechanical and
rigorous way. So, before getting into the details, let us see if we
cannot state these simple rules in a way that will dispel this air of
mystery and allow you the really understand what it is all about.
\bye
\def\a{\b}
\def\b{A\def\a{B\def\a{\b}}}
\a\ Now reverse it. \a\ Now what is it? \a\ And now what? \a\